<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="shortcut icon" href="/favicon.ico"> <link href="/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="/css/animate.min.css" rel="stylesheet">
    <link href="/css/style.min.css?v=4.0.0" rel="stylesheet">
	<!-- 搜索样式CSS-Start -->
	<link href="/common/css/searchStyle.css" rel="stylesheet">
	<!-- 搜索样式CSS-End -->
	<!-- 下拉 -->
	<link href="/css/plugins/chosen/chosen.css" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="row row-lg">
                <div class="col-sm-12">
                    <!-- Example Events -->
                    <div class="example-wrap">
                        <div class="btn-group hidden-xs" id="exampleTableEventsToolbar" role="group">
                             <form id="seachform">
                             <input type="hidden" id="emplAttr"/>
	                             <div class="seach-context">
											 <label>联系人名称 :</label><input id="name"placeholder="请输入客户名称"/>
											 <label>联系人SMS :</label><input id="sms"placeholder="请输入SMS"/>
											 <label>联系人邮箱:</label><input id="email"placeholder="请输入客户邮箱"/>
					             </div>
					            <button id="resetBtn" type="button" class="btn btn-w-m btn-white">重置</button>
								<button id="searchBtn" type="button" class="btn btn-w-m btn-white">查询</button>
								<button id="confirm" type="button" class="btn btn-w-m btn-success">确定</button>
							</form>
                        </div>
                        <table id="systemCompanyEmplTable">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
	<!-- End Panel Other -->
	<script src="/js/jquery.min.js?v=2.1.4"></script>
	<script src="/js/bootstrap.min.js?v=3.3.5"></script>
	<script src="/js/content.min.js?v=1.0.0"></script>
	<script src="/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
	<script src="/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
	<script src="/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="/common/petecat.js"></script>
	<script src="/common/bootstrap-table-treetable.js"></script>
	<script src="/common/bootstrap-table.js"></script>
	<script src="/common/form.js"></script>
	<!-- HTML之间的传值 -->
	<script src="/js/jquery.params.js"></script>
	<script src="/js/plugins/layer-pc/layer.js"></script>
	<script src="/js/jquery.cookie.js"></script>
    <!-- 组件通用 -->
    <script src="/common/frame.js"></script>
    <!-- 下拉 -->
	<script src="/js/plugins/chosen/chosen.jquery.js"></script>
<script type="text/javascript">
checkPowerAndJump("QRYCOMPANYEMPL");
    $(function(){
    	//获取页面中点击的联系人员
    	var emplAttr = GetQueryString("emplAttr");
    	$('#resetBtn').click(function(){
    		$('#seachform').resetForm();
    	});
        $('#searchBtn').click(function(){
            refreshGrid('systemCompanyEmplTable');
        });
        var table = {};
    	table['id'] = 'systemCompanyEmplTable';
    	table['url'] = SSO_URL+'/system/companyempl/queryCompanyEmplByPage',
    	table['pagination'] = true,
    	table['queryParams'] = function(params){
    		var queryParams = queryBaseParams(params);
    		queryParams['sms'] = $('#sms').val();
            queryParams['email'] = $('#email').val();
            queryParams['name'] = $('#name').val();
            if(parent.$("#arrayId").val() != ""){
            	queryParams['emplIds'] = parent.$("#arrayId").val();
            }
            queryParams['status'] = "1";
            return queryParams;
    	};
    	table['columns'] = [{
	    	 field:'name',
	         title:'联系人名称'
			},{
	    	 field:'sms',
	         title:'SMS'
		    },{
	    	 field:'email',
	         title:'联系人邮箱'
	    	}];
    	initTable(table);
    	
    	//点击某一行选中
    	var emplRowArray = new Array();
    	$('#systemCompanyEmplTable').on('click-row.bs.table', function (e,row,element) {
    		row['smsRule'] = 1;
    		row['emailRule'] = 1;
    		row['emplAttr'] = emplAttr;
    		if(!isExistsByKey(emplRowArray,row.id)){
    			$(element).css({"background-color":"#6699ff","color":"#fff"});
	    		emplRowArray.push({rowKey:row.id,rowVal:row});
    		}else{
    			$(element).css({"background-color":"#fff","color":'black'});
    			removeBykey(emplRowArray,row.id);
    		}
    	});
    	
    	//点击确定将值传到下一个页面
    	$("#confirm").click(function(){
    		//至少选择一位预警人员
			if(emplRowArray.length == 0){
					layer.open({
						title:'提示',
						content:'至少选择一位预警人员'
					});
			 return;
			}
    		var emplName = "";
    		var emplId = "";
    		//设置id数组
    		$.each(emplRowArray,function(index,item){
    			emplName += item.rowVal.name+",";
    			emplId += item.rowVal.id+",";
    		});
    		var parentInputName = parent.$("[emplAttr='"+emplAttr+"']");
    		//设置内容
    		parentInputName.val(parentInputName.val()+emplName);
			//设置id
			parentInputName.next().val(parentInputName.next().val()+emplId);
    		parentEmplRowArray = parent.parentEmplRowArray;
   			if(!isExistsByKey1(parentEmplRowArray,emplAttr)){
   				parentEmplRowArray.push({emplAttr,emplRowArray});
       		}else{
       			for(var i = 0;i<parentEmplRowArray.length;i++){
	       			if(parentEmplRowArray[i].emplAttr==emplAttr){
	       				parentEmplRowArray[i].emplRowArray = parentEmplRowArray[i].emplRowArray.concat(emplRowArray);
	       			}
       			}
       		}
    		if(parentEmplRowArray.length == 0){
    			parentEmplRowArray.push({emplAttr,emplRowArray});
    		}
    		//循环赋值到Table中
			var parentTable = parent.$('#warningEmplTable');
			for(var i = 0;i<emplRowArray.length;i++){
				parentTable.bootstrapTable('append', emplRowArray[0].rowVal)
			}
			//人员删除的时候知道替换上面的那个文本框
			parent.$("#attrId").val(emplAttr);
			//关闭本窗口
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
    	});
    });
    
    //通过Key判断是否存在
	function isExistsByKey(array, obj){
	    for (var i = 0; i < array.length; i++){
	        if (array[i].rowKey == obj)//如果要求数据类型也一致，这里可使用恒等号===
	            return true;
	    }
	    return false;
	}
    //判断是否第二次点击
	function isExistsByKey1(array, obj){
	    for (var i = 0; i < array.length; i++){
	        if (array[i].emplAttr == obj)//如果要求数据类型也一致，这里可使用恒等号===
	            return true;
	    }
	    return false;
	}
	
	//通过Key移除row
	function removeBykey(array,key){
		$.each(array,function(index,item){
			if(key == item.rowKey){
				array.splice(index,1);
				return false;
			}
		});
	}
	
</script>
</body>
</html>